import 'dart:convert';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wanxiang_app/bean/normal_entity.dart';
import 'package:wanxiang_app/pages/index.dart';
import 'package:wanxiang_app/pages/msg/chat.dart';
import 'package:wanxiang_app/utils/constant.dart';
import '../../bean/service_list_entity.dart';
import '../../utils/network_manager.dart';
import '../../utils/network_url.dart';
import '../../utils/toast_util.dart';
import '../../widgets/myappbar.dart';
import 'package:get/get.dart';

import '../bean/agreement_entity.dart';
import '../utils/user_util.dart';
import '../widgets/privacy_dialog.dart';

class WelcomeController extends GetxController{

  @override
  void onInit() {
    super.onInit();

  }
  @override
  void onClose() {
    super.onClose();
  }
  late BuildContext mContext;
  @override
  void onReady() {
    super.onReady();
    _getService();
  }

  void _goIndex(){
    Future.delayed(const Duration(milliseconds: 2000), () {

      Get.off(()=>IndexPage());
    });
  }
  void _sendChannel() {
    // 创建一个 MethodChannel 对象，通道名称为 "app.channel.notifaction"
    const platform = MethodChannel('app.channel.notifaction');
// 调用原生平台方法
    try {
      var sharedData = platform.invokeMethod('notifactionJson');
      // 处理原生平台返回值
    } on PlatformException catch (e) {
      // 处理调用原生平台方法时出现的错误
    }
  }

  void _getService() {
    if (UserUtil.getFirst().isNotEmpty) {
      _sendChannel();
      _goIndex();
      return;
    }
    DioManager.getInstance().post(ServiceUrl.privacy, null, (data) async {
      AgreementEntity bannerEntity = AgreementEntity.fromJson(jsonDecode(data));
      showDialog(
          context: mContext,
          builder: (BuildContext context1) {
            return MyAPrivacyDialog(
              callBack: (String content) {
                if (content == 'cancel') {
                  Navigator.pop(context1);
                  exit(0);
                } else if (content == 'sure') {
                  _sendChannel();
                  UserUtil.saveFirst('init');
                  _goIndex();
                  Navigator.pop(context1);
                }
              },
              sureText: "已阅读并同意",
              msg: bannerEntity.data.content,
            );
          });
      update();
    }, (error) {
      ToastUtil.show(error);
    });

  }
}


class WelcomePage extends GetView<WelcomeController>  {
  late WelcomeController controller ;
  late BuildContext context ;
  @override
  Widget build(BuildContext context1) {
    Get.lazyPut<WelcomeController>(() => WelcomeController());
    controller = Get.find();
    context = context1;

    return GetBuilder<WelcomeController>(
      builder: (_){
        controller.mContext = context1;
        return  Scaffold(
            backgroundColor: Colors.white,
            body:_content());
      },
    );

  }
  Widget _content(){
    return  Container(
      height: Get.height,
      width: Get.width,
      decoration: const BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/launcher.png'),
            fit: BoxFit.cover,
          )
      ),
        );
  }


}